Skip to content

Conversation

@belimawr
Copy link
Contributor

@belimawr belimawr commented Nov 20, 2025

What does this PR do?

This PR adds two helpers for tests:

  • fs.TempDir creates a temporary folder that is kept when tests fails.
  • NewLogFile creates a file that can be used as output for a logger or a a process. Methods to search strings in the file are also provided

Why is it important?

It helps us to keep temporary files and logs generated by tests when the test fails for easier debugging.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works

## Author's Checklist
## Related issues

@belimawr belimawr self-assigned this Nov 20, 2025
@belimawr belimawr added the Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team label Nov 20, 2025
//
// When tests are run with -v, the temporary directory absolute
// path will be logged.
func TempDir(t *testing.T, path ...string) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: what's the difference with the stdlib t tmpdir ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TempDir creates a folder that when the test fails is kept and the path logged. This allows us to debug the state the test was running both locally and on CI.

We have number of tests in Beats and Elastic Agent that create configuration files, logs to be ingested, etc. When those tests fail and all state is gone, debugging becomes extremely hard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have similar functionality spread across many tests/test frameworks in Beats and Elastic Agent, I'm trying to consolidate them.

I've heavily needed and relayed on this functionality in the past couple of weeks to debug a number of tests, after duplicating the code in a few PRs I realised I needed it in a central place, with a single implementation.

@belimawr belimawr marked this pull request as ready for review November 21, 2025 15:19
@belimawr belimawr requested a review from a team as a code owner November 21, 2025 15:19
@belimawr belimawr requested review from andrzej-stencel and leehinman and removed request for a team November 21, 2025 15:19
@pierrehilbert pierrehilbert requested review from rdner and removed request for andrzej-stencel November 21, 2025 16:32
//
// When tests are run with -v, the temporary directory absolute
// path will be logged.
func TempDir(t *testing.T, path ...string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of copying so much of the code that exists in t.TempDir(). Can we use t.TempDir and then just have the cleanup function copy or archive the contents in the case of a failure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I like that the temp dir is already created on its final destination, so no matter how wrong the test goes, the files are kept.

Copying/archiving the temp dir after a test failure adds another point of failure and complexity that I'd like to avoid.

I'm not a fan of copying so much of the code that exists in t.TempDir()

I haven't copied much of it, well, the bit I copied was only the part sanitising the test name to create the folder.

// subsequent call to WaitLogsContains will only check logs not yet evaluated.
// msgAndArgs should be a format string and arguments that will be printed
// if the logs are not found, providing additional context for debugging.
func (l *LogFile) WaitLogsContains(t testing.TB, s string, timeout time.Duration, msgAndArgs ...any) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've needed it in different places, like Elastic Agent. So this PR is a first attempt to centralise some helpers in a single place.

@belimawr belimawr requested a review from AndersonQ December 3, 2025 17:36
@elasticmachine
Copy link
Collaborator

💚 Build Succeeded

History

cc @belimawr

@belimawr
Copy link
Contributor Author

belimawr commented Dec 3, 2025

@leehinman @kruskall did you folks have a chance to see my replies to your comments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants